package cn.tta.controller.user;

import cn.tta.common.result.PageResult;
import cn.tta.common.result.Result;
import cn.tta.pojo.dto.OrdersPaymentDTO;
import cn.tta.pojo.dto.OrdersSubmitDTO;
import cn.tta.pojo.vo.OrderPaymentVO;
import cn.tta.pojo.vo.OrderSubmitVO;
import cn.tta.pojo.vo.OrderVO;
import cn.tta.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * C端-订单接口
 */
@RestController
@RequestMapping("/user/order")
@Slf4j
public class UserOrderController {

    @Autowired
    private OrdersService ordersService;


    /**
     * 用户催单
     * @param id
     * @return
     */
    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable Long id) {
        ordersService.reminder(id);
        return Result.success();
    }

    /**
     * 再来一单
     * @param id
     * @return
     */
    @PostMapping("/repetition/{id}")
    public Result repetition(@PathVariable Long id) {
        ordersService.repetition(id);
        return Result.success();
    }


    /**
     * 取消订单
     * @param id
     * @return
     * @throws Exception
     */
    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable("id") Long id) throws Exception {
        ordersService.userCancelById(id);
        return Result.success();
    }

    /**
     * 订单详情
     * @param id
     * @return
     */
    @GetMapping("/orderDetail/{id}")
    public Result orderDetail(@PathVariable("id") Long id) {
        OrderVO orderVO = ordersService.orderDetail(id);
        return Result.success(orderVO);
    }

    /**
     * 历史订单查询
     * @param page
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/historyOrders")
    public Result page(int page,int pageSize, Integer status) {
        PageResult pageResult = ordersService.pageQueryUser(page,pageSize,status);
        return Result.success(pageResult);
    }

    /**
     *  订单支付
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    public Result payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}",ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = ordersService.payment(ordersPaymentDTO);
        return Result.success(orderPaymentVO);
    }

    /**
     * 用户下单
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    public Result submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("用户下单：{}",ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = ordersService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }
}
